class Video < ActiveRecord::Base
  belongs_to :event
  belongs_to :user
  belongs_to :video_status
  
  has_many :video_approvals, :dependent => :destroy
  has_many :video_comments, :dependent => :destroy
  
  
  def self.reporter_videos_list(user, filters = {})
	@where = "user_id = '#{ user.id }' AND"

	if filters[:text] && !filters[:text].empty?
		@where += " (title LIKE '%#{ filters[:text] }%' OR" +
					" body LIKE '%#{ filters[:text] }%'" +					
					") AND"
	end
	
	if !@where.empty?
		@where += " 1=1"
	end
	
	self.find(:all, :order => "videos.updated_at DESC", :conditions => @where)
  end
  
  def self.filter( filters = {} )
  @where = ''

  if filters[:text] && !filters[:text].empty?
    @where += " (title LIKE '%#{ filters[:text] }%')"
  end
  
  if filters[:user_id] && !filters[:user_id].empty?
    @where += " and user_id = '#{ filters[:user_id] }' "
  end
  
  if filters[:video_status_id] && !filters[:video_status_id].empty?
    @where += "AND video_status_id = '#{ filters[:video_status_id] }' "
  end

  
  self.where(@where)
end

 def self.videos_approving
    Video.where("video_status_id = #{VideoStatus.get("APPROVING").id}")
  end
  
  def self.video_new
    Video.where("video_status_id = #{VideoStatus.get("NEW").id}")
  end
  
  def reporter_edit?(current_user)
    video_status != VideoStatus.get('APPROVING') && video_status != VideoStatus.get('APPROVED') && user == current_user
  end
  
  def renderVideo
    "upload/videos/event/#{self.id.to_s + "_" + self.path}"
  end
  
  def upload(file, thumb)
    #Update @image.picture 

    @file_path = "upload/videos/event/#{self.id.to_s + "_" + file.original_filename}"
    File.open(@file_path, "wb") { |f| f.write(file.read) }
    
    #create thumbnails
    createVideoThumbnail(thumb)
    
    # createThumbnails
    
    # self.width = ImageTools.getWidth(@file_path)
    # self.height = ImageTools.getHeight(@file_path)
    self.video_type = ImageTools.getType(@file_path)
    
    self.save
  end
  
  def deleteVideo
    @file_path = "upload/videos/event/#{self.id.to_s + "_" + self.path}"
    File.delete(@file_path) if File.exist?(@file_path)
    
    deleteVideoThumbnail
    
  end
  
  def createVideoThumbnail(thumb)
    @file_path = "upload/videos/event/thumbnails/#{self.id.to_s + "_" + thumb.original_filename}"
    File.open(@file_path, "wb") { |f| f.write(thumb.read) }
    
    ImageTools.createThumbnail(@file_path, "upload/videos/event/thumbnails/#{self.id.to_s + "_thumb_" + thumb.original_filename}", 55, 55)
    ImageTools.createThumbnail(@file_path, "upload/videos/event/thumbnails/#{self.id.to_s + "_thumb75_" + thumb.original_filename}", 75, 75)
  end
  
  def deleteVideoThumbnail
    # deleteThumbnails
    # @thumb_path = "upload/videos/event/thumbnails/#{self.id.to_s + "_" + self.thumbnail}"
    # File.delete(@thumb_path) if File.exist?(@thumb_path)
    
    # @thumb_path = "upload/videos/event/thumbnails/#{self.id.to_s + "_thumb_" + self.thumbnail}"
    # File.delete(@thumb_path) if File.exist?(@thumb_path)
    
    # @thumb_path = "upload/videos/event/thumbnails/#{self.id.to_s + "_thumb75_" + self.thumbnail}"
    # File.delete(@thumb_path) if File.exist?(@thumb_path)
    
    Dir.foreach("upload/videos/event/thumbnails"){|x| File.delete("upload/videos/event/thumbnails/"+x)  if x=~/^#{self.id.to_s}(.*)/ }
  end
  
  def renderThumbnail
    "upload/videos/event/thumbnails/#{self.id.to_s + "_thumb_" + self.thumbnail}"
  end
  
  def renderThumbnail75
    "upload/videos/event/thumbnails/#{self.id.to_s + "_thumb75_" + self.thumbnail}"
  end
  
  
end
